#!/usr/bin/env python
# coding=utf-8
from flask import Blueprint
from flask import render_template

from mynest.service.lightsv import Light
from mynest.service.locksv import LockSV

nestlogin = Blueprint('smart', __name__)


@nestlogin.route('/index')
def index():
    return render_template('index.html')


@nestlogin.route('/open/<username>')
def openTheDoor(username=None):
    '''
    开门控制
    :param username: 用户名
    :return: 页面
    '''

    if username != 'lixin':
        return render_template('error.html', error="Waring : Account Error !")

    lockSV = LockSV()
    lockSV.openLock()
    return render_template('ok.html', ok="Alert : The door opened !")


@nestlogin.route('/lock')
def lock():
    '''
    锁门控制
    :return: 页面
    '''
    lockSV = LockSV()
    lockSV.lock()

    return render_template('ok.html', ok="Alert : The door locked !")


@nestlogin.route('/light/all/on')
def lightAllOn():
    '''
    开灯控制
    :return: 页面
    '''
    Light(18).on()
    Light(24).on()
    Light(25).on()
    return render_template('ok.html', ok="Alert : Light ON !")


@nestlogin.route('/light/all/off')
def lightAllOff():
    '''
    关灯控制
    :return: 页面
    '''
    Light(18).off()
    Light(24).off()
    Light(25).off()
    return render_template('ok.html', ok="Alert : Light OFF !")


@nestlogin.route('/light/door/on')
def lightDoorOn():
    '''
    开门口灯
    :return: 页面
    '''
    Light(18).on()
    return render_template('ok.html', ok="Alert : Light ON !")


@nestlogin.route('/light/door/off')
def lightDoorOff():
    '''
    关门口灯
    :return: 页面
    '''
    Light(18).off()
    return render_template('ok.html', ok="Alert : Light OFF !")


@nestlogin.route('/light/live/on')
def lightLiveOn():
    '''
    开厅灯
    :return: 页面
    '''
    Light(24).on()
    return render_template('ok.html', ok="Alert : Light ON !")


@nestlogin.route('/light/live/off')
def lightLiveOff():
    '''
    关厅灯
    :return: 页面
    '''
    Light(24).off()
    return render_template('ok.html', ok="Alert : Light OFF !")


@nestlogin.route('/light/bedroom/on')
def lightBedroomOn():
    '''
    开卧室灯
    :return: 页面
    '''
    Light(25).on()
    return render_template('ok.html', ok="Alert : Light ON !")


@nestlogin.route('/light/bedroom/off')
def lightBedroomOff():
    '''
    关卧室灯
    :return: 页面
    '''
    Light(25).off()
    return render_template('ok.html', ok="Alert : Light OFF !")
